using System;
using System.Data;
using System.Xml;



namespace OR.Lazarus.Library.DataTier.Compiled
{
   public class TableDetail : OR.Lazarus.Library.DataTier.Compiled.Lazarus.TableDetail
   {
		public ProjectDetail Project
		{
			get
			{
				return ( ProjectDetail )base.ParentEntity;
			}
		}

		public virtual FieldDetail GetPKField()
		{
			FieldDetail v_oReturn = null;

			foreach ( FieldDetail v_oField in this.Fields )
			{
				if ( v_oField.IsPrimaryKey )
				{
					v_oReturn = v_oField;

					break;
				}
			}

			return ( v_oReturn );
		}

		public bool IsRoot
		{
			get
			{
				return ( this.GetIsRoot() );
			}
		}

		protected virtual bool GetIsRoot()
		{
			return ( this.Implements.Count == 0 );
		}

		public virtual TableCollection GetDependencyChain( TableCollection pContext )
		{
			TableCollection	vReturn	= new TableCollection();

			this.GetDependencyChain( pContext, vReturn );

			return ( vReturn );
		}

		public virtual void GetDependencyChain( TableCollection pContext, TableCollection pResult )
		{
//			TableDetail	vTable	= null;

			pResult.AddAt( 0, this );
		}

		public virtual TableCollection GetInheritanceChain( TableCollection pContext )
		{
			TableCollection	vReturn	= new TableCollection();

			this.GetInheritanceChain( pContext, vReturn );

			return ( vReturn );
		}

		protected virtual void GetInheritanceChain( TableCollection pContext, TableCollection pResult )
		{
			TableDetail	vTable	= null;

			pResult.AddAt( 0, this );

			foreach ( ImplementDetail vImplement in this.Implements )
			{
				vTable = pContext[ vImplement.Ref ];

				if ( vTable == null )
				{
					throw new ApplicationException( String.Format( "Table [{0}] has an Implements reference to Table [{1}], which does not exist in the model.", this.Name,  vImplement.Ref ) );
				}

				vTable.GetInheritanceChain( pContext, pResult );
			}
		}

		public virtual FieldDetail GetClusteredField()
		{
			FieldDetail v_oReturn = null;

			foreach ( FieldDetail v_oField in this.Fields )
			{
				if ( v_oField.IsClustered )
				{
					v_oReturn = v_oField;

					break;
				}
			}

			return ( v_oReturn );
		}
	}
}
   